import { Component, OnInit } from '@angular/core';
import { ThreeLibsService } from 'src/app/core/service/three-libs.service';

@Component({
  selector: 'app-threat-location',
  templateUrl: './threat-location.component.html',
  styleUrls: ['./threat-location.component.scss']
})
export class ThreadLocationComponent implements OnInit {
  barOption = {
    legend: {},
    tooltip: {},
    dataset: {
      source: [
        ['product', '2015', '2016', '2017'],
        ['Matcha Latte', 43.3, 85.8, 93.7],
        ['Milk Tea', 83.1, 73.4, 55.1],
        ['Cheese Cocoa', 86.4, 65.2, 82.5],
        ['Walnut Brownie', 72.4, 53.9, 39.1]
      ]
    },
    xAxis: { type: 'category' },
    yAxis: {},
    // Declare several bar series, each will be mapped
    // to a column of dataset.source by default.
    series: [
      { type: 'bar' },
      { type: 'bar' },
      { type: 'bar' }
    ]
  };

  lineOption = {
    title: {
      text: ''
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['mysql', 'Tomcat', 'linux', 'JDBC', 'MAC']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: 'mysql',
        type: 'line',
        stack: '总量',
        data: [120, 132, 101, 134, 90, 230, 210]
      },
      {
        name: 'Tomcat',
        type: 'line',
        stack: '总量',
        data: [220, 182, 191, 234, 290, 330, 310]
      },
      {
        name: 'linux',
        type: 'line',
        stack: '总量',
        data: [150, 232, 201, 154, 190, 330, 410]
      },
      {
        name: 'JDBC',
        type: 'line',
        stack: '总量',
        data: [320, 332, 301, 334, 390, 330, 320]
      },
      {
        name: 'MAC',
        type: 'line',
        stack: '总量',
        data: [820, 932, 901, 934, 1290, 1330, 1320]
      }
    ]
  };

  pieOption = {
    series: [
      {
        name: '资产信息',
        type: 'pie',
        radius: '55%',
        data: [
          { value: 235, name: 'mysql' },
          { value: 274, name: 'Tomcat' },
          { value: 310, name: 'linux' },
          { value: 335, name: 'JDBC' },
          { value: 400, name: 'MAC' }
        ]
      }
    ]
  };


  customize = false;

  isConfirmLoading = false;
  isVisible = false;
  radioValue = 'A';

  allChecked = false;
  indeterminate = false;
  displayData = [];

  pageIndex = 1;      // 页码标识
  pageSize = 10;      // 每页展示多少数据 —— 10条
  total = 1;          // 总共1页
  dataSet = [];       // 数据集合
  loading = true;     // 加载
  sortValue = null;   // 种类值
  sortKey = null;     // 种类关键值
  filterGender = [
    { text: 'male', value: 'male' },
    { text: 'female', value: 'female' }
  ];
  searchGenderList: string[] = [];

  /**
   * 在中间显示
   */
  showModal(): void {
    this.isVisible = true;
    this.searchData();
  }
  /**
   * 触发 OK 事件
   */
  handleOk(): void {
    this.isConfirmLoading = true;
    setTimeout(() => {
      this.isVisible = false;
      this.isConfirmLoading = false;
    }, 3000);
    // 加载表格中的数据
    this.searchData();
  }
  /**
   * 触发 取消事件
   */
  handleCancel(): void {
    this.isVisible = false;
  }

  /**
   * 搜索数据
   * @param reset 重置
   */
  searchData(reset: boolean = false): void {
    if (reset) {
      this.pageIndex = 1;
    }
    this.loading = true;
    this.threelibsservice.getUsers(this.pageIndex, this.pageSize, this.sortKey, this.sortValue,
      this.searchGenderList).subscribe((data: any) => {
        this.loading = false;
        this.total = 200;
        this.dataSet = data.results;
      });
  }

  sort(sort: { key: string, value: string }): void {
    this.sortKey = sort.key;
    this.sortValue = sort.value;
    this.searchData();
  }


  constructor(private threelibsservice: ThreeLibsService) { }

  updateFilter(value: string[]): void {
    this.searchGenderList = value;
    this.searchData(true);
  }

  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
    this.displayData = $event;
    this.refreshStatus();
  }


  /**
   * 刷新状态
   */
  refreshStatus(): void {
    const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked === true);
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  /**
   * 全选方法
   */
  checkAll(value: boolean): void {
    this.displayData.forEach(data => {
      if (!data.disabled) {
        data.checked = value;
      }
    });
    // 刷新状态
    this.refreshStatus();
  }

  /* onConfigChange(): void {
    if (this.customize) {
      this.nzEmptyService.setDefaultContent(this.customTpl); // tslint:disable-line:no-any
    } else {
      this.nzEmptyService.resetDefault();
    }
  } */

  ngOnInit() {
    console.log('说执行就执行');
    this.showModal();
  }

}
